//
//  CLVistorView.swift
//  weibo001
//
//  Created by CL on 16/7/9.
//  Copyright © 2016年 CL. All rights reserved.
//

import UIKit
import SnapKit


class CLVistorView: UIView {
  //登录或注册执行的闭包
    var loginClosure: (() ->())?
    
      //MARK   --懒加载
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.grayColor()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        //多行显示
        label.numberOfLines = 0
        label.textAlignment = .Center
        
      return label
        
    }()
    
    
  //注册
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        
        //设置文字颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
       return button
    } ()
    
    
    //登录
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        
        //设置文字颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    } ()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加视图设置约束
    private  func setupUI() {
       //设置访客颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //使用之前必须先添加
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //使用snapKit,
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }

      //MARK   --点击事件
    @objc private func registerButtonAction() {
        print("访客视图的注册按钮点击错了")
        loginClosure?()
    }
    
    
    @objc private func loginButtonAction() {
        print("访客视图的登录按钮点击错了")
        loginClosure?()
    }
    
    //开启动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
       
        //目的地
        animation.toValue = 2 * M_PI
        
        //动画时长
        animation.duration = 20
        
        //重复次数
        animation.repeatCount = MAXFLOAT
        
        //动画执行后不释放
        animation.removedOnCompletion = false
       
        //执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //提供修改信息的访客视图
    func updateVisitorViewInfo(title: String?, imageName: String?) {
        
        if let t = title, imgName = imageName {
    
            messageLabel.text = t
            iconImageView.image = UIImage(named: imgName)
            //隐藏旋转视图
            cycleImageView.hidden = true
            
        }else {
             cycleImageView.hidden = false
            //开启动画
            startAnimation()
        }
        
        
        
        
    }
    
    
    
    
    
}















